﻿using JshRCSWebApi.Infrastructure.Domain.DTO.SysVehicle;
using JshRCSWebApi.Infrastructure;
using JshRCSWebApi.Util.VehicleTool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using JshRCSWebApi.Service.SysVehicle;
using JshRCSWebApi.Infrastructure.Domain.DTO.SysVehicle.VehicleState;
using Newtonsoft.Json;
using JshRCSWebApi.Util;
using JshRCSWebApi.Infrastructure.Domain.VO;
using JshRCSWebApi.Infrastructure.Domain.VO.SysVehicle.VehicleState;

namespace JshRCSWebApi.ServiceImpl.SysVehicle
{
    public class VehicleStateServiceImpl: VehicleStateService
    {
        private int vehiclePort = 19204;
          
        /// <summary>
        /// 查询机器人信息 1000     
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_info_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1000;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_info_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人运行信息 1002      
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_run_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1002;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_run_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人位置 1004       
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_loc_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1004;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_loc_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人速度 1005        
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_speed_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1005;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_speed_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人的被阻挡状态 1006         
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_block_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1006;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_block_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人电池状态 1007          
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_battery_req(RequestDTO<robot_status_battery_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1007;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_battery_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人扫图状态 1025           
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_slam_req(RequestDTO<robot_status_slam_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1025;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_slam_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人当前所在区域 1011            
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_area_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1011;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_area_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人急停状态 1012             
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_emergency_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1012;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_emergency_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人导航状态 1020            
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_task_req(RequestDTO<robot_status_task_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1020;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_task_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人任务状态 1110             
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_task_status_package_req(RequestDTO<robot_status_task_status_package_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1110;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_task_status_package_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人定位状态 1021              
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_reloc_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1021;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_reloc_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人地图载入状态 1022               
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_loadmap_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1022;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_loadmap_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询顶升机构状态 1027                
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_jack_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1027;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_jack_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人报警状态 1050                 
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_alarm_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1050;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_alarm_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询当前控制权所有者 1060                  
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_current_lock_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1060;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_current_lock_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人载入的地图以及储存的地图 1300                   
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_map_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1300;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_map_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询机器人当前载入地图中的站点信息 1301                    
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_station_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1301;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_station_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 查询任意两点之间的路径 1303              
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_status_get_path_req(RequestDTO<robot_status_get_path_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 1303;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<robot_status_get_path_VO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

    }
}
